/*
 * KriteriaLaporan.java
 *
 * Created on September 12, 2008, 2:39 AM
 */
package com.indragunawan.restobiz.app;

import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.DefaultListModel;
import org.jdesktop.application.Action;

/**
 *
 * @author  igoens
 */
public class KriteriaLaporan extends javax.swing.JDialog {

    private static final long serialVersionUID = -2812189386297382716L;

    private int i;
    private ArrayList<Integer> kriteriaShift = new ArrayList<Integer>();
    private GeneralConfig cfg = new GeneralConfig();
    private Date TAwal;
    private Date TAkhir;
    private boolean bukaLaporan;
    private boolean periodeOnly;

    /** Creates new form KriteriaLaporan */
    public KriteriaLaporan(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        fillShiftList();
        initTanggal();
        initShift();
    }

    public void setPeriodeOnly(Boolean value) {
        this.periodeOnly = value;
    }

    private Boolean getPeriodeOnly() {
        return periodeOnly;
    }

    private void setAllKriteria() {
        setKriteriaShift();
        setKriteriaPeriode();
    }

    private void setConfirmOpen(Boolean Confirm) {
        this.bukaLaporan = Confirm;
    }

    public Boolean getConfirmOpen() {
        return this.bukaLaporan;
    }

    public void fillShiftList() {
        DefaultListModel model = new DefaultListModel();

        model.addElement("SEMUA");
        for (i = 1; i <= cfg.getMaxShift(); i++) {
            model.addElement("SHIFT " + String.valueOf(i));
        }

        shiftList.setModel(model);
    }

    private void setKriteriaPeriode() {
        TAwal = tanggalAwalField.getDate();
        TAkhir = tanggalAkhirField.getDate();
    }

    public Date getTanggalAwal() {
        return TAwal;
    }

    public Date getTanggalAkhir() {
        return TAkhir;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        laporanButtonGroup = new javax.swing.ButtonGroup();
        kriteriaPanel = new javax.swing.JPanel();
        shiftPanel = new javax.swing.JPanel();
        shiftScrollPane = new javax.swing.JScrollPane();
        shiftList = new javax.swing.JList();
        periodePanel = new javax.swing.JPanel();
        tanggalAwalLabel = new javax.swing.JLabel();
        tanggalAwalField = new org.jdesktop.swingx.JXDatePicker();
        tanggalAkhirLabel = new javax.swing.JLabel();
        tanggalAkhirField = new org.jdesktop.swingx.JXDatePicker();
        laporanHarianRadioButton = new javax.swing.JRadioButton();
        laporanPeriodeRadioButton = new javax.swing.JRadioButton();
        batalButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(com.indragunawan.restobiz.app.MainApp.class).getContext().getResourceMap(KriteriaLaporan.class);
        setTitle(resourceMap.getString("kriteriaLaporan.title")); // NOI18N
        setName("kriteriaLaporan"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        kriteriaPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("kriteriaPanel.border.border.title")))); // NOI18N
        kriteriaPanel.setName("kriteriaPanel"); // NOI18N

        shiftPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        shiftPanel.setName("shiftPanel"); // NOI18N

        shiftScrollPane.setName("shiftScrollPane"); // NOI18N

        shiftList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "SEMUA", "SHIFT 1", "SHIFT 2", "SHIFT 3" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        shiftList.setName("shiftList"); // NOI18N
        shiftScrollPane.setViewportView(shiftList);

        javax.swing.GroupLayout shiftPanelLayout = new javax.swing.GroupLayout(shiftPanel);
        shiftPanel.setLayout(shiftPanelLayout);
        shiftPanelLayout.setHorizontalGroup(
            shiftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(shiftPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(shiftScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)
                .addContainerGap())
        );
        shiftPanelLayout.setVerticalGroup(
            shiftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(shiftPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(shiftScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)
                .addContainerGap())
        );

        periodePanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        periodePanel.setName("periodePanel"); // NOI18N

        tanggalAwalLabel.setDisplayedMnemonic('T');
        tanggalAwalLabel.setLabelFor(tanggalAwalField);
        tanggalAwalLabel.setText(resourceMap.getString("tanggalAwalLabel.text")); // NOI18N
        tanggalAwalLabel.setName("tanggalAwalLabel"); // NOI18N

        tanggalAwalField.setEnabled(false);
        tanggalAwalField.setName("tanggalAwalField"); // NOI18N

        tanggalAkhirLabel.setDisplayedMnemonic('a');
        tanggalAkhirLabel.setLabelFor(tanggalAkhirField);
        tanggalAkhirLabel.setText(resourceMap.getString("tanggalAkhirLabel.text")); // NOI18N
        tanggalAkhirLabel.setName("tanggalAkhirLabel"); // NOI18N

        tanggalAkhirField.setEnabled(false);
        tanggalAkhirField.setName("tanggalAkhirField"); // NOI18N

        javax.swing.GroupLayout periodePanelLayout = new javax.swing.GroupLayout(periodePanel);
        periodePanel.setLayout(periodePanelLayout);
        periodePanelLayout.setHorizontalGroup(
            periodePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(periodePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(periodePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tanggalAwalField, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)
                    .addComponent(tanggalAkhirLabel)
                    .addComponent(tanggalAkhirField, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)
                    .addComponent(tanggalAwalLabel))
                .addContainerGap())
        );
        periodePanelLayout.setVerticalGroup(
            periodePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(periodePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tanggalAwalLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tanggalAwalField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tanggalAkhirLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tanggalAkhirField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        laporanButtonGroup.add(laporanHarianRadioButton);
        laporanHarianRadioButton.setMnemonic('H');
        laporanHarianRadioButton.setSelected(true);
        laporanHarianRadioButton.setText(resourceMap.getString("laporanHarianRadioButton.text")); // NOI18N
        laporanHarianRadioButton.setName("laporanHarianRadioButton"); // NOI18N
        laporanHarianRadioButton.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                laporanHarianRadioButtonStateChanged(evt);
            }
        });

        laporanButtonGroup.add(laporanPeriodeRadioButton);
        laporanPeriodeRadioButton.setMnemonic('P');
        laporanPeriodeRadioButton.setText(resourceMap.getString("laporanPeriodeRadioButton.text")); // NOI18N
        laporanPeriodeRadioButton.setName("laporanPeriodeRadioButton"); // NOI18N
        laporanPeriodeRadioButton.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                laporanPeriodeRadioButtonStateChanged(evt);
            }
        });

        javax.swing.GroupLayout kriteriaPanelLayout = new javax.swing.GroupLayout(kriteriaPanel);
        kriteriaPanel.setLayout(kriteriaPanelLayout);
        kriteriaPanelLayout.setHorizontalGroup(
            kriteriaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(kriteriaPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(kriteriaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(shiftPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(laporanHarianRadioButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(kriteriaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(laporanPeriodeRadioButton)
                    .addComponent(periodePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        kriteriaPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {periodePanel, shiftPanel});

        kriteriaPanelLayout.setVerticalGroup(
            kriteriaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(kriteriaPanelLayout.createSequentialGroup()
                .addGroup(kriteriaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(laporanHarianRadioButton)
                    .addComponent(laporanPeriodeRadioButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(kriteriaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(shiftPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(periodePanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(17, Short.MAX_VALUE))
        );

        kriteriaPanelLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {periodePanel, shiftPanel});

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(com.indragunawan.restobiz.app.MainApp.class).getContext().getActionMap(KriteriaLaporan.class, this);
        batalButton.setAction(actionMap.get("closeKriteriaLaporan")); // NOI18N
        batalButton.setMnemonic('B');
        batalButton.setText(resourceMap.getString("batalButton.text")); // NOI18N
        batalButton.setName("batalButton"); // NOI18N
        batalButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                batalButtonKeyPressed(evt);
            }
        });

        okButton.setAction(actionMap.get("setKriteriaLaporan")); // NOI18N
        okButton.setMnemonic('O');
        okButton.setText(resourceMap.getString("okButton.text")); // NOI18N
        okButton.setName("okButton"); // NOI18N
        okButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                okButtonKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(kriteriaPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(okButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(batalButton)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {batalButton, okButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(kriteriaPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(batalButton)
                    .addComponent(okButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void laporanPeriodeRadioButtonStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_laporanPeriodeRadioButtonStateChanged
    tanggalAwalField.setEnabled(laporanPeriodeRadioButton.isSelected());
    tanggalAkhirField.setEnabled(laporanPeriodeRadioButton.isSelected());
}//GEN-LAST:event_laporanPeriodeRadioButtonStateChanged

private void laporanHarianRadioButtonStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_laporanHarianRadioButtonStateChanged
    shiftList.setEnabled(laporanHarianRadioButton.isSelected());
}//GEN-LAST:event_laporanHarianRadioButtonStateChanged

private void okButtonKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_okButtonKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        setKriteriaLaporan();
    }
}//GEN-LAST:event_okButtonKeyPressed

private void batalButtonKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_batalButtonKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        closeKriteriaLaporan();
    }
}//GEN-LAST:event_batalButtonKeyPressed

private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
    if (getPeriodeOnly()) {
        laporanPeriodeRadioButton.setSelected(true);
        laporanHarianRadioButton.setEnabled(false);
    }
}//GEN-LAST:event_formWindowActivated

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton batalButton;
    private javax.swing.JPanel kriteriaPanel;
    private javax.swing.ButtonGroup laporanButtonGroup;
    private javax.swing.JRadioButton laporanHarianRadioButton;
    private javax.swing.JRadioButton laporanPeriodeRadioButton;
    private javax.swing.JButton okButton;
    private javax.swing.JPanel periodePanel;
    private javax.swing.JList shiftList;
    private javax.swing.JPanel shiftPanel;
    private javax.swing.JScrollPane shiftScrollPane;
    private org.jdesktop.swingx.JXDatePicker tanggalAkhirField;
    private javax.swing.JLabel tanggalAkhirLabel;
    private org.jdesktop.swingx.JXDatePicker tanggalAwalField;
    private javax.swing.JLabel tanggalAwalLabel;
    // End of variables declaration//GEN-END:variables

    @SuppressWarnings("unchecked")
    private void setKriteriaShift() {
        int[] Nilai = shiftList.getSelectedIndices();

        kriteriaShift.clear();

        if (Nilai.length == 1) {
            if (Nilai[0] == 0) {
                for (i = 1; i <= cfg.getMaxShift(); i++) {
                    kriteriaShift.add(i);
                }
            } else {
                kriteriaShift.add(Nilai[0]);
            }
        } else {
            for (i = 0; i <= Nilai.length - 1; i++) {
                kriteriaShift.add(Nilai[i]);
            }
        }
    }

    @SuppressWarnings("unchecked")
    public ArrayList getKriteriaShift() {
        if (kriteriaShift.size() == 0) {
            kriteriaShift.add(0);
        }
        return kriteriaShift;
    }

    private void initTanggal() {
        Date today = new Date();
        tanggalAwalField.setDate(today);
        tanggalAkhirField.setDate(today);
    }

    private void initShift() {
        shiftList.setSelectedIndex(0);
    }

    @Action
    public void closeKriteriaLaporan() {
        this.setConfirmOpen(false);
        this.dispose();
    }

    @Action
    public void setKriteriaLaporan() {
        if (laporanHarianRadioButton.isSelected()) {
            initTanggal();
        } else {
            initShift();
        }
        setAllKriteria();
        this.setConfirmOpen(true);
        this.dispose();
    }
}
